//
//  ViewController.m
//  CityPicker
//
//  Created by rattanchen on 18/8/9.
//  Copyright © 2017年 rattanchen. All rights reserved.
//

#import "ViewController.h"
#import "RattanCityPicker.h"
#import "SQSelectAddressViewController.h"
#import "XXCityPickerView.h"

@interface ViewController ()<SQSelectAddressViewControllerDelegate,XXCityPickerViewDelegate>

@property(nonatomic,strong)UILabel *lblShow;

@property(nonatomic,strong)UIButton *btn;

@property(nonatomic,strong)XXCityPickerView *cityPicker;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initBaseUI];
}

#pragma mark - init
//添加标签
-(void)initBaseUI{
    [self lblShow];
    [self btn];
}

#pragma mark - Method
-(void)btnAction:(UIButton *)sender{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"地址选择器" message:@"请选择：" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"XXCityPickerView" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self cityPicker];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"SQSelectAddress" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self sqSelectAddressPockerView];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"RattanCityPicker" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self rattanCity];
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [self presentViewController:alert animated:YES completion:^{
    }];
}

#pragma mark -
#pragma mark - XXCityPickerView

- (XXCityPickerView *)cityPicker{
    if (_cityPicker == nil){
        // 初始化  设置类别   1省  2省-市  3省-市-区
        _cityPicker = [[XXCityPickerView alloc]initWithComponents:3];
        // 设置代理
        _cityPicker.delegate = self;
        [self.view addSubview:_cityPicker];
    }
    return  _cityPicker;
}

// ---- 【XXCityPickerViewDelegate】
// 取消
- (void)XXCityPickerViewLeftAction{
    [self removeCityPickerView];
}
// 完成
- (void)XXCityPickerViewRightAction:(XXCityPickerView *)picker andCity:(NSString *)city andData:(NSMutableDictionary *)dic{
    _lblShow.text = city;
    [self removeCityPickerView];
    NSLog(@"🐼🐼🐼XXCityPickerView:\n city= %@   dic= %@",city,dic);
}

-(void)removeCityPickerView{
    if (_cityPicker) {
        [_cityPicker removeFromSuperview];
        _cityPicker = nil;
    }
}


#pragma mark -
#pragma mark - SQSelectAddress


-(void)sqSelectAddressPockerView{
    SQSelectAddressViewController *selectAddressVC = [[SQSelectAddressViewController alloc] init];
    selectAddressVC.delegate = self;
    selectAddressVC.animating = NO;
    selectAddressVC.footerBg_color = [UIColor greenColor];
    selectAddressVC.headerBg_color = [UIColor greenColor];
    selectAddressVC.title_font = [UIFont systemFontOfSize:25];
    selectAddressVC.titleText_color = [UIColor orangeColor];
    [self presentViewController:selectAddressVC animated:YES completion:nil];
}

// 【SQSelectAddressViewControllerDelegate】
- (void)selectAddressViewController:(SQSelectAddressViewController *)selectController selectedDetailAdreess:(NSString *)address townCode:(NSString *)townCode{
    _lblShow.text = [NSString stringWithFormat:@"%@",address];
    NSLog(@"🐼🐼SQSelectAddress:\n地址=%@  编码=%@",address,townCode);
}


#pragma mark -
#pragma mark - RattanCityPicker

-(void)rattanCity{
    RattanCityPicker *picker=[[RattanCityPicker alloc] init];
    picker.animation_dur = 0.3;
    picker.toolViewBg_color =[UIColor greenColor];
    picker.componentFir_font = [UIFont systemFontOfSize:16];
    picker.componentStrFir_color = [UIColor redColor];;
    picker.componentBgFir_color = [UIColor lightGrayColor];
    picker.selectLblStr_font = [UIFont systemFontOfSize:20];
    picker.selectLblStr_color = [UIColor redColor];
    //点击确定按钮代码块调用
    picker.sureBtnBlock = ^(){

    };
    //获取选择的城市的信息
    [picker cityPikerGetSelectCity:^(NSMutableDictionary *dicSelectCity) {
        [self setTextLbl:dicSelectCity];
        NSLog(@"🐼🐼RattanCityPicker:\n%@",dicSelectCity);
    }];
}


#pragma mark -
#pragma mark - lazyload
//获取结果
-(void)setTextLbl:(NSMutableDictionary *)dicSelectCity{
    _lblShow.textColor = [UIColor orangeColor];
    _lblShow.text = [NSString stringWithFormat:@"%@ %@ %@ %@",dicSelectCity[@"Province"],dicSelectCity[@"City"],dicSelectCity[@"District"], dicSelectCity[@"cityCode"]];
}

//结果显示标签
-(UILabel *)lblShow{
    if (!_lblShow) {
        _lblShow = [[UILabel alloc]init];
        _lblShow.frame = CGRectMake(25, 70, self.view.frame.size.width - 50, 30);
        _lblShow.textColor = [UIColor blackColor];
        _lblShow.text = @"将在这里显示获取信息";
        [self.view addSubview:_lblShow];
    }
    return _lblShow;
}

//按钮
-(UIButton *)btn{
    if (!_btn) {
        _btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        _btn.frame = CGRectMake(20, 200, 200, 50);
        [_btn setTitle:@"点击获取地址信息" forState:UIControlStateNormal];
        _btn.backgroundColor = [UIColor lightGrayColor];
        [_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_btn];
    }
    return _btn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
